var tmpattr = [];

$(function() {
	$('#li_mdf').addClass('active open');
	$('#topomonitor').addClass('active');

	init();
	var temp;

	clicktable = function(row) {
		temp = row.id;
		$.ajax({
			type : 'post',
			url : 'toposelect',
			dataType : 'text',
			data : {
				"topoid" : row.id
			},
			success : function(msg) {
				var tmp = JSON.parse(msg);
				tmpattr = tmp.nodeAttrArray;
				// console.log(tmpattr);
				nodeattrlist = tmpattr;

				document.getElementById("mySavedModel").value = msg;
				load();
			},
			error : function(msg) {
				console.log('failed');
			}
		});
	};

	var project = $('#project').val();
	// console.log(project);

	var trList = $("#nodetable").children("tr");
	for (var i = 0; i < trList.length; i++) {
		var s = trList[i];
		// console.log(trList[i].getAttribute('id'));
		if (s.getAttribute('id') == project) {
			clicktable(s);
		}
	}

	if (project == 'NONE') {
		var first = $('#nodetable').children(":first").get(0);
		clicktable(first);
	}

	$("#SaveButton").click(
			function() {
				// console.log('save');
				document.getElementById("mySavedModel").value = myDiagram.model
						.toJson();
				myDiagram.isModified = false;
				var json = JSON.parse(myDiagram.model.toJson());
				// console.log(myDiagram.model.toJson())

				var dclass = json.class;
				var linkFromPortIdProperty = json.linkFromPortIdProperty;
				var linkToPortIdProperty = json.linkToPortIdProperty;

				var nodeDataArray = json.nodeDataArray;
				var linkDataArray = json.linkDataArray;

				// console.log(nodeattrlist);

				$.ajax({
					type : 'post',
					url : 'savetopology',
					dataType : 'text',
					data : {
						"project" : temp,
						"dclass" : dclass,
						"linkFromPortIdProperty" : linkFromPortIdProperty,
						"linkToPortIdProperty" : linkToPortIdProperty,
						"nodeDataArray" : JSON.stringify(nodeDataArray),
						"linkDataArray" : JSON.stringify(linkDataArray),
						"nodeAttrList" : JSON.stringify(nodeattrlist)
					},
					success : function(msg) {
						if (msg == '1') {
							alert("保存成功");
						}
					},
					error : function(msg) {
						console.log('failed')
					}
				});

				// clear
				// nodeattrlist = [];
			});

	$("#LoadButton").click(function() {
		load();
	});

});
